﻿/*-----------------------------------------------------------------------------------------
   Author               Date            Description
   ----------------------------------------------------------------------------------------
   Steven Applegate     01/30/2011      Initial creation
 ----------------------------------------------------------------------------------------*/

using System.Web;
using System.Web.Mvc;
using System.Text;

namespace NCPA.Common
{
    public static class MenuHelpers
    {
        /// <summary>
        /// Author: Steven Applegate (01/30/2011)
        /// Description: Renders HTML for the Admin menu
        /// </summary>
        public static string AdminMenu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            // create opening unordered list tag
            sb.Append("<ul id='nav'>");

            // render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            foreach (SiteMapNode node in topLevelNodes)
            {
                if (node.ChildNodes.Count > 0)
                {
                    sb.AppendLine("<li class='parent'>");
                    sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title));
                    sb.AppendLine("<ul class='subnav'>");
                    foreach (SiteMapNode childNode in node.ChildNodes)
                    {
                        sb.AppendFormat("<li><a href='{0}'>{1}</a></li>", childNode.Url, helper.Encode(childNode.Title));
                    }
                    sb.AppendLine("</ul>");
                    sb.AppendLine("</li>");
                }
                else
                    sb.AppendFormat("<li><a href='{0}'>{1}</a></li>", node.Url, helper.Encode(node.Title));
            }

            // close unordered list tag
            sb.Append("</ul>");

            return sb.ToString();
        }

    }
}